﻿using Module.NumberGame.Common;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Module.NumberGame.ViewModels
{
    public class NumberBoxViewModel : BindableBase
    {
        private readonly IEventAggregator Ea;

        private int pieceValue;
        public int PieceValue
        {
            get { return pieceValue; }
            set
            {
                SetProperty(ref pieceValue, value);
                MakeHidden = value == 0;
            }
        }

        private bool makeHidden;
        public bool MakeHidden
        {
            get { return makeHidden; }
            set { SetProperty(ref makeHidden, value); }
        }
        public NumberBoxViewModel(IEventAggregator ea)
        {
            this.Ea = ea;
            Ea.GetEvent<NumberBoardDataEvent>().Subscribe(BoardDataChanged);
            Ea.GetEvent<NumberMessageEvent>().Subscribe(PieceMove);
            PieceValue = 1;
        }

        private bool moveLeft;
        public bool MoveLeft
        {
            get { return moveLeft; }
            set { SetProperty(ref moveLeft, value); }
        }
        private bool moveRight;
        public bool MoveRight
        {
            get { return moveRight; }
            set { SetProperty(ref moveRight, value); }
        }
        private bool moveUp;
        public bool MoveUp
        {
            get { return moveUp; }
            set { SetProperty(ref moveUp, value); }
        }
        private bool moveDown;
        public bool MoveDown
        {
            get { return moveDown; }
            set { SetProperty(ref moveDown, value); }
        }
        private void PieceMove(NumberMessage obj)
        {
            MoveLeft = false; MoveRight = false; MoveUp = false; MoveDown = false;
            if (Math.Abs(obj.MouseDisX) > Math.Abs(obj.MouseDisY)) // 水平移动量大于垂直移动量时，只水平移动
            {
                if (obj.MouseDisX < 0) MoveLeft = true; else moveRight = true;
            }
            else if (obj.MouseDisY < 0) MoveUp = true; else MoveDown = true;
        }

        private void BoardDataChanged(NumberMessage obj)
        {
            //PieceValue = obj.BoardDataMessage[0,0]; 
            PieceValue = (PieceValue == 0) ? 2 : 0;
        }
    }
}
